<template>
	<!-- 管理商品 -->
	
	<view class="container">
		<button class="add" type="primary" size="mini"  @click="add_goods">添加商品</button>
		<uni-table border  stripe emptyText="暂无更多数据" >
		    <!-- 表头行 -->
		    <uni-tr >
		        <uni-th align="center">商品名称</uni-th>
		        <uni-th align="center">单价</uni-th>
				<uni-th align="center">类别</uni-th>
		        <uni-th align="left">设置</uni-th>
		    </uni-tr>
		    <!-- 表格数据行 -->
		    <uni-tr v-for="(item,index) in goods">
		        <uni-td class="goods_name">{{item.spmc}}</uni-td>
		        <uni-td class="goods_price">{{item.spdj}}</uni-td>
				<uni-td class="goods_type">{{item.basetypeTitle}}</uni-td>
		        <uni-td class="device">
				<button class="edi_left" type="primary" size="mini" @click="edit_goods(item)">编辑</button>
				<button class="delete_right" type="warn" size="mini" @click="delete_goods(item.goodsID)">删除</button>
				</uni-td>
		    </uni-tr>
		</uni-table>
		
	</view>
</template>

<script>
	import { createNamespacedHelpers } from 'vuex'
	const { mapState, mapActions, mapGetters } = createNamespacedHelpers('goods')
	
	export default {
		data() {
			return {
				reFresh: "",
			}
		},
		computed: {
			...mapGetters(['goods'])
		},
		watch: {
			//监听reFresh,如果有修改就执行监听器
			reFresh: function() {
		        //调接口刷新数据
			
				this.$store.dispatch('goods/get_goods')
			}
		},
		onLoad() {
			this.$store.dispatch('goods/get_goods')
		},
		methods: {
			add_goods(){
				uni.navigateTo({
					url:'/pages/addGoods/addGoods'
				})
			},
			async delete_goods(id){
				console.log('delete_goods',id)
				uni.showModal({
				    title: '提示',
				    content: '您确定要删除吗',
				    success: (res)=> {
						console.log(res)
				        if (res.confirm) {
				            console.log('用户点击确定');
						
						this.$request({
								url:'/supermarket/goods/removeGoodsById/'+id,
								method:"DELETE",
							}).then((res)=>{
								console.log('res',res)
								this.$store.dispatch('goods/get_goods')
							})
				        }
				    }
				})
				
				/* let result = await this.$request({
					url:'/supermarket/deletespxx/'+id
				})
				console.log('result',result)
				this.$store.dispatch('getGoods') */
			},
			
			edit_goods(info){
				// console.log('info',JSON.parse(JSON.stringify(info)))
				console.log('info',info)
				// console.log('info',JSON.stringify(info))
				uni.navigateTo({
					// url:'/pages/editGoods/editGoods?information='+JSON.stringify(info) //info是一个对象
					url:'/pages/editGoods/editGoods?goodsID='+info.goodsID+'&spmc='+info.spmc+'&basetypeId='+info.basetypeId+'&appendtypeId='+info.appendtypeId+'&spdj='+info.spdj+'&spzk='+info.spzk+'&sptpcflj='+info.sptpcflj+'&sptype='+info.sptype   
				})
			}
		},

	}
</script>

<style lang="scss">
	.container{
		width: 130%;
	}
	.add{
			margin-left: 15rpx;
			margin-top: 10rpx;
		}
	.goods_name{
		width: 15%;
	}
	.goods_price{
		width: 5%;
	}
	.goods_type{
		width: 10%;
	}
	.device{
		width: 35%;
	}
	.edi_left{	
	}
	.delete_right{
	}
	.special{
			// border: 1rpx solid #f00;
			width: 30%;
			display: flex;
			align-items: center;
		}
		
	
	
</style>

